home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / vsoup11.zip / main.cc < prev    next >
C/C++ Source or Header  |  1996-09-02  |  11KB  |  427 lines

  1. //  $Id: main.cc 1.14 1996/09/02 13:22:50 hardy Exp $
  2. //
  3. //  This progam/module was written by Hardy Griech based on ideas and
  4. //  pieces of code from Chin Huang (cthuang@io.org).  Bug reports should
  5. //  be submitted to rgriech@ibm.net.
  6. //
  7. //  This file is part of soup++ for OS/2.  Soup++ including this file
  8. //  is freeware.  There is no warranty of any kind implied.  The terms
  9. //  of the GNU Gernal Public Licence are valid for this piece of software.
  10. //
  11. //  Fetch mail and news using POP3 and NNTP into a SOUP packet.
  12. //
  13.  
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <signal.h>
  19. #include <getopt.h>
  20.  
  21. //
  22. //  für TCPOS2.INI-Leserei
  23. //
  24. #ifdef OS2
  25. #define INCL_WINSHELLDATA
  26. #define INCL_DOSPROCESS    ////
  27. #define INCL_DOSEXCEPTIONS ////
  28. #include <os2.h>
  29. #endif
  30.  
  31. #define DEFGLOBALS
  32. #include "areas.hh"
  33. #include "global.hh"
  34. #include "mts.hh"
  35. #include "news.hh"
  36. #include "pop3.hh"
  37. #include "reply.hh"
  38.  
  39.  
  40. static enum { RECEIVE, SEND, CATCHUP } mode = RECEIVE;
  41. static char doMail = 1;
  42. static char doSummary = 0;
  43. static int  catchupCount;
  44. #ifdef OS2
  45. static char doIni = 1;        // if TRUE, read TCPOS2.INI file
  46. #endif
  47.  
  48.  
  49.  
  50. static void setFiles( void )
  51. {
  52. #ifdef OS2
  53.     sprintfT(newsrcFile, "%s/newsrc", homeDir);
  54.     sprintfT(killFile, "%s/kill", homeDir);
  55. #else
  56.     sprintfT(newsrcFile, "%s/.newsrc", homeDir);
  57.     sprintfT(killFile, "%s/.kill", homeDir);
  58. #endif
  59. }   // setFiles
  60.  
  61.  
  62.  
  63. static void getUrlInfo( const char *url, serverInfo &info )
  64. //
  65. //  get an URL in the form [user[:passwd]@]host
  66. //
  67. {
  68.     char user[512];
  69.     char passwd[512];
  70.     char host[512];
  71.  
  72.     if (sscanfT(url,"%[^@]@%s",user,host) == 2) {
  73.     if (sscanfT(url,"%[^:]:%[^@]@%s",user,passwd,host) == 3)
  74.         xstrdup( &info.passwd,passwd );
  75.     else
  76.         sscanfT(url,"%[^@]@%s",user,host );
  77.     xstrdup( &info.user,user );
  78.     xstrdup( &info.host,host );
  79.     }
  80.     else
  81.     xstrdup( &info.host,url );
  82.  
  83. #ifdef TRACE_ALL
  84.     printfT( "getUrlInfo(%s): %s:%s@%s\n",url,info.user,info.passwd,info.host );
  85. #endif
  86. }   // getUrlInfo
  87.  
  88.  
  89.  
  90. static void usage( const char *fmt, ... )
  91. {
  92.     va_list ap;
  93.  
  94.     if (fmt != NULL) {
  95.     char buf[BUFSIZ];
  96.     
  97.     va_start( ap,fmt );
  98.     vsprintf( buf,fmt,ap );
  99.     va_end( ap );
  100.     areas.mailPrintf1( 1,"%s\n",buf );
  101.     }
  102.  
  103.     fputsT("VSoup v1.1ß (rg010996) - transfer POP3 mail and NNTP news to SOUP\n", stderr);
  104.     fprintfT(stderr, "usage: %s [options] [URLs]\n",progname );
  105.     fputsT("  URL:  (nntp|pop3|smtp)://[userid[:password]@]host\n",stderr );
  106.     fputsT("global options:\n",stderr );
  107.     fputsT("  -h dir   Set home directory\n", stderr);
  108. #ifdef OS2
  109.     fputsT("  -i       Do not read 'Internet Connection for OS/2' settings\n", stderr);
  110. #endif
  111.     fputsT("  -m       Do not get mail\n", stderr);
  112.     fputsT("  -M       generate status mail\n", stderr);
  113.     fputsT("  -n       Do not get news\n", stderr);
  114.     fputsT("  -r       Read only mode.  Do not delete mail or update newsrc\n", stderr);
  115.     fputsT("  -s       Send replies\n", stderr);
  116.  
  117.     fputsT("news reading options:\n", stderr);
  118.     fputsT("  -a       Add new newsgroups to newsrc file\n", stderr);
  119.     fputsT("  -c[n]    Mark every article as read except for the last n [default: 10]\n", stderr);
  120.     fputsT("  -k n     Set maximum news packet size in kBytes\n", stderr);
  121.     fputsT("  -K file  Set kill file\n", stderr);
  122.     fputsT("  -l n     Kill articles longer than n lines\n", stderr);
  123.     fputsT("  -N file  Set newsrc file\n", stderr);
  124. #if defined(OS2)  &&  defined(__MT__)
  125.     fprintfT(stderr,"  -t n     Number of threads [1..%d, standard: %d]\n",
  126.          MAXNNTPTHREADS,DEFNNTPTHREADS );
  127. #endif
  128.     fputsT("  -u       Create news summary\n", stderr);
  129.     fputsT("  -x       Do not process news Xref headers\n", stderr);
  130.     areas.closeAll( 1 );
  131.     exit( EXIT_FAILURE );
  132. }   // usage
  133.  
  134.  
  135.  
  136. static void parseCmdLine( int argc, char **argv )
  137. {
  138.     int c;
  139.     int i;
  140.  
  141.     while ((c = getopt(argc, argv, "?ac::h:iK:k:l:MmN:nrst:ux")) != EOF) {
  142.     switch (c) {
  143.     case '?':
  144.         usage( NULL );
  145.         break;
  146.     case 'a':
  147.         doNewGroups = 1;
  148.         break;
  149.     case 'c':
  150.         mode = CATCHUP;
  151.         if (optarg != NULL)
  152.         catchupCount = atoi(optarg);
  153.         else
  154.         catchupCount = 10;
  155.         break;
  156.     case 'h':
  157.         homeDir = optarg;
  158.         setFiles();
  159.         break;
  160. #ifdef OS2
  161.     case 'i':
  162.         doIni = 0;
  163.         break;
  164. #endif
  165.     case 'K':
  166.         strcpy(killFile, optarg);
  167.         killFileOption = 1;
  168.         break;
  169.     case 'k':
  170.         maxBytes = atol(optarg) * 1000L;
  171.         break;
  172.     case 'l':
  173.         maxLines = atoi(optarg);
  174.         break;
  175.     case 'M':
  176.         genStsMail = 1;
  177.         break;
  178.     case 'm':
  179.         doMail = 0;
  180.         break;
  181.     case 'N':
  182.         strcpy(newsrcFile, optarg);
  183.         break;
  184.     case 'n':
  185.         doNews = 0;
  186.         break;
  187.     case 'r':
  188.         readOnly = 1;
  189.         break;
  190.     case 's':
  191.         mode = SEND;
  192.         break;
  193. #if defined(OS2)  &&  defined(__MT__)
  194.     case 't':
  195.         maxNntpThreads = atoi(optarg);
  196.         if (maxNntpThreads < 1  ||  maxNntpThreads > MAXNNTPTHREADS)
  197.         usage( "ill num of threads %s",optarg );
  198.         break;
  199. #endif
  200.     case 'u':
  201.         doNews = 1;
  202.         doSummary = 1;
  203.         break;
  204.     case 'x':
  205.         doXref = 0;
  206.         break;
  207.     default:
  208.         usage( NULL );
  209.     }
  210.     }
  211.  
  212.     //
  213.     //  get the URLs
  214.     //
  215.     for (i = optind;  i < argc;  i++) {
  216.     if (strnicmp("smtp://",argv[i],7) == 0)
  217.         getUrlInfo( argv[i]+7, smtpInfo );
  218.     else if (strnicmp("pop3://",argv[i],7) == 0)
  219.         getUrlInfo( argv[i]+7, pop3Info );
  220.     else if (strnicmp("nntp://",argv[i],7) == 0)
  221.         getUrlInfo( argv[i]+7, nntpInfo );
  222.     else
  223.         usage( "ill URL %s",argv[i] );
  224.     }
  225. }   // parseCmdLine
  226.  
  227.  
  228.  
  229. #ifdef OS2
  230. static void readTcpIni (void)
  231. {
  232.     HAB hab;
  233.     HINI hini;
  234.     char *etc;
  235.     char buf[BUFSIZ];
  236.     char curConnect[20] = "";
  237.     char host[40]       = "";
  238.     char domain[40]     = "";
  239.  
  240.     etc = getenv("ETC");
  241.     if (etc == NULL) {
  242.     fputsT("Must set ETC\n", stderr);
  243.     exit(EXIT_FAILURE);
  244.     }
  245.     sprintfT(buf, "%s\\TCPOS2.INI", etc);
  246.  
  247.     hab = WinInitialize(0);
  248.     hini = PrfOpenProfile(hab, buf);
  249.     if (hini == NULLHANDLE) {
  250.     fprintfT(stderr, "Cannot open profile %s\n", buf);
  251.     exit(EXIT_FAILURE);
  252.     }
  253.  
  254.     PrfQueryProfileString(hini, "CONNECTION", "CURRENT_CONNECTION", NULL,
  255.                           curConnect, sizeof(curConnect));
  256.  
  257.     PrfQueryProfileString(hini, curConnect, "HOSTNAME", NULL, host,
  258.               sizeof(host));
  259.     PrfQueryProfileString(hini, curConnect, "DOMAIN_NAME", NULL, domain,
  260.               sizeof(domain));
  261.     sprintfT(buf, "%s.%s", host, domain);
  262.     xstrdup( &hostName,buf );
  263.  
  264.     PrfQueryProfileString(hini, curConnect, "POPSRVR", NULL, buf, sizeof(buf));
  265.     xstrdup( &pop3Info.host,buf );
  266.     PrfQueryProfileString(hini, curConnect, "POP_ID", NULL, buf, sizeof(buf));
  267.     xstrdup( &pop3Info.user,buf );
  268.     xstrdup( &smtpInfo.user,buf );
  269.     xstrdup( &nntpInfo.user,buf );
  270.     PrfQueryProfileString(hini, curConnect, "POP_PWD", NULL, buf, sizeof(buf));
  271.     xstrdup( &pop3Info.passwd,buf );
  272.     xstrdup( &smtpInfo.passwd,buf );
  273.     xstrdup( &nntpInfo.passwd,buf );    
  274.  
  275.     PrfQueryProfileString(hini, curConnect, "DEFAULT_NEWS", NULL, buf, sizeof(buf));
  276.     xstrdup( &nntpInfo.host,buf );
  277.  
  278.     PrfQueryProfileString(hini, curConnect, "MAIL_GW", NULL, buf, sizeof(buf));
  279.     xstrdup( &smtpInfo.host,buf );
  280.     
  281.     PrfCloseProfile(hini);
  282.     WinTerminate(hab);
  283.  
  284. #ifdef DEBUG
  285.     printfT( "TCPOS2.INI information:\n" );
  286.     printfT( "-----------------------\n" );
  287.     printfT( "host/domain:  %s/%s\n", host,domain );
  288.     printfT( "hostName:     %s\n", hostName );
  289.     printfT( "defNews:      %s\n", nntpInfo.host );
  290.     printfT( "popServer:    %s\n", pop3Info.host );
  291.     printfT( "popUser:      %s\n", pop3Info.user );
  292. //    printfT( "popPassword:  %s\n", pop3Info.passwd );
  293.     printfT( "mailGateway:  %s\n", smtpInfo.host );
  294.     printfT( "-----------------------\n" );
  295. #endif
  296. }   // readTcpIni
  297. #endif
  298.  
  299.  
  300.  
  301. static void signalHandler( int sig )
  302. {
  303.     char msg[100];
  304.  
  305. #ifndef NDEBUG
  306.     printfT( "\nHallo exception\n" );
  307. #endif
  308.     sprintf( msg,"signal %d received